﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TXTextControl;

namespace QuanLyNganHangDe.GUI
{
    static class TextControlUtils
    {
        public static TextControl CreateTextControl(System.Windows.Forms.Panel pnlParent)
        {
            var rtb = new TXTextControl.TextControl();
            pnlParent.Controls.Add(rtb);
            pnlParent.CreateControl();
            rtb.CreateControl();
            rtb.Clear();
            rtb.Select(0, 0);
            return rtb;
        }

        public static TextControl CreateTextControl()
        {
            var rtb = new TXTextControl.TextControl();
            rtb.CreateControl();
            rtb.Clear();
            rtb.Select(0, 0);
            return rtb;
        }

        public static string GetRtfCode(this TextControl rtb)
        {
            string rtfcode = null;
            rtb.Save(out rtfcode, StringStreamType.RichTextFormat);
            return rtfcode;
        }

        public static void SetRtfCode(this TextControl rtb, string rtfCode, bool autoReplaceNullContent = true)
        {
            if (string.IsNullOrEmpty(rtfCode))
            {
                rtb.Text = "KHÔNG CÓ DỮ LIỆU";
            }
            else
            {
                //rtb.Clear();
                rtb.Load(rtfCode, StringStreamType.RichTextFormat);
            }
        }

        /// <summary>
        /// Phương thức mới bổ sung
        /// 1) Kiểm tra xem rtfCode có phải là tên file hợp lệ hay không -> ưu tiên nạp RtfCode từ file trước
        /// 2) Nếu không phải thì cho lưu xuống file
        /// </summary>
        /// <param name="rtb"></param>
        /// <param name="rtfCodeInfo"></param>
        /// <param name="autoReplaceNullContent"></param>
        public static void SetRtfCode2(this TextControl rtb, string rtfCodeInfo, bool autoReplaceNullContent = true)
        {
            try
            {
                //rtfCodeInfo = rtf
                //if (System.IO.File.Exists(rtfCodeInfo))
                //{
                //    #region rtfCodeInfo là đường dẫn hợp lệ
                //    var rtfCode = System.IO.File.ReadAllText(rtfCodeInfo);
                //    if (string.IsNullOrEmpty(rtfCode))
                //    {
                //        rtb.Text = "KHÔNG CÓ DỮ LIỆU";
                //    }
                //    else
                //    {
                //        //rtb.Clear();
                //        rtb.Load(rtfCode, StringStreamType.RichTextFormat);
                //    }
                //    #endregion
                //}
                //else
                {
                    #region rtfCodeInfo chính là rtfCode
                    var rtfCode = rtfCodeInfo;
                    if (string.IsNullOrEmpty(rtfCode))
                    {
                        rtb.Text = "KHÔNG CÓ DỮ LIỆU";
                    }
                    else
                    {
                        //rtb.Clear();
                        rtb.Load(rtfCode, StringStreamType.RichTextFormat);
                    }
                    #endregion
                }
            }
            catch (Exception exc)
            {
                rtb.SetPlainText("Không nạp được dữ liệu do lỗi: " + exc.ToString());
                rtb.Text += "\r\n\r\nHãy thử khôi phục lại nội dung gốc";
            }
        }

        public static string GetPlainText(this TextControl rtb)
        {
            string text = null;
            rtb.Save(out text, StringStreamType.PlainText);
            return text;
        }

        public static void SetPlainText(this TextControl rtb, string text)
        {
            rtb.Load(text, StringStreamType.PlainText);
        }

        public static void SaveToRtfFile(this TextControl rtb, string filename)
        {
            rtb.Save(filename, StreamType.RichTextFormat);
        }

        public static int FindAndReplace(this TextControl rtb, string tag, string value, int startPos = 0)
        {
            if (startPos < 0)
                startPos = 0;
            var idx = rtb.Find(tag, startPos, TXTextControl.FindOptions.NoMessageBox);
            if (idx > 0)
            {
                rtb.Select(idx, tag.Length);
                rtb.Selection.Text = value;
                return idx + value.Length;
            }
            else
                return -1;
        }

        public static void FindAndReplace(this TextControl rtb, Dictionary<string, string> dict)
        {
            var sb = new StringBuilder();
            foreach (var key in dict.Keys)
            {
                var idxFound = FindAndReplace(rtb, key, dict[key]);
                if (idxFound < 0)
                    sb.AppendFormat("Không tìm thấy {0} để thay thế {1}\n", key, dict[key]);
            }
            if (sb.Length > 0)
                DialogUtils.ShowMessage(sb.ToString());
        }

        public static void FindAndReplace(this TextControl dest, string tag, TextControl source)
        {
            string rtfCode;
            source.Save(out rtfCode, TXTextControl.StringStreamType.RichTextFormat);
            var idx = dest.Find(tag, 0, TXTextControl.FindOptions.NoMessageBox);
            dest.Select(idx, tag.Length);
            dest.Selection.Load(rtfCode, TXTextControl.StringStreamType.RichTextFormat);
        }

        public static void Append(this TextControl rtb, TextControl source, AppendSettings appendSettings = AppendSettings.None)
        {
            string rtfCode = source.GetRtfCode();
            rtb.Append(rtfCode, TXTextControl.StringStreamType.RichTextFormat, appendSettings);
        }

        public static void AppendRtfCode(this TextControl rtb, string rtfcode, AppendSettings appendSettings = AppendSettings.None)
        {
            rtb.Append(rtfcode, StringStreamType.RichTextFormat, appendSettings);
        }

    }
}
